package com.wenwei.sharebox.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wenwei.sharebox.R;
import com.wenwei.sharebox.bean.BaseGoodsBean;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/7/7.
 */
public class CommonShareAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //type 0:租用 1：购买

    private Context mContext;
    private CallBack mCallBack;
    public interface CallBack {
        void onItemClick(int id);
    }

    public static enum ITEM_TYPE {
        ITEM_TYPE_BUY,
        ITEM_TYPE_RENTS,
    }

    public CommonShareAdapter(CallBack callBack){
        this.mCallBack = callBack;
    }


    private List<BaseGoodsBean.DataBean> mList;

    public void setGoodsData(List<BaseGoodsBean.DataBean> list) {
        this.mList = list;
    }

    @Override
    public int getItemViewType(int position) {
        return mList.get(position).getIsBuy() == 1 ? ITEM_TYPE.ITEM_TYPE_BUY.ordinal() : ITEM_TYPE.ITEM_TYPE_RENTS.ordinal();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        if (viewType == ITEM_TYPE.ITEM_TYPE_BUY.ordinal()) {
            return new ShareBuyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_share_buy, null));
        } else {
            return new ShareRentsViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_share_rents, null));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ShareBuyViewHolder) {
            ((ShareBuyViewHolder) holder).itemTitle.setText(mList.get(position).getShopName());
            ((ShareBuyViewHolder) holder).itemPrice.setText("￥" + mList.get(position).getMoney());
            Glide.with(mContext).load(mList.get(position).getImg()).error(R.drawable.banner).into(((ShareBuyViewHolder) holder).itemImg);
            ((ShareBuyViewHolder) holder).itemLocation.setText(mList.get(position).getAddressName());
        } else if (holder instanceof ShareRentsViewHolder){
            ((ShareRentsViewHolder) holder).itemTitle.setText(mList.get(position).getShopName());
            ((ShareRentsViewHolder) holder).itemJiFen.setText(mList.get(position).getIntegralNum()+"");
            Glide.with(mContext).load(mList.get(position).getImg()).error(R.drawable.banner).into(((ShareRentsViewHolder) holder).itemImg);
            ((ShareRentsViewHolder) holder).itemLocation.setText(mList.get(position).getAddressName());
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCallBack.onItemClick(mList.get(position).getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public static class ShareBuyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.item_img)
        ImageView itemImg;
        @Bind(R.id.item_title)
        TextView itemTitle;
        @Bind(R.id.item_location)
        TextView itemLocation;
        @Bind(R.id.item_price)
        TextView itemPrice;
        @Bind(R.id.item_operate)
        TextView itemOperate;

        public ShareBuyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public static class ShareRentsViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.item_img)
        ImageView itemImg;
        @Bind(R.id.item_title)
        TextView itemTitle;
        @Bind(R.id.item_location)
        TextView itemLocation;
        @Bind(R.id.item_ji_fen)
        TextView itemJiFen;
        @Bind(R.id.item_operate)
        TextView itemOperate;
        public ShareRentsViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


}
